#!/usr/bin/env python

import anydbm
from sys import exit

ADMIN_DB = anydbm.open('Admin_Accounts','c')
MANAGER_PASSWORD = ('discovery')

def ctrlPanel():
	print "Welcome to your pannel!"
	print "Your choices is to \n[1] create admin db if not exist or edite the current db.\n[2] Search for adminstrator password .\n[3] Delete adminstrator. \n[4] Exit"
	print "So, type the number of the operation you want to perform!"
	task = int(raw_input('Enter your choice:'))
	if task == 1:
		add(ADMIN_DB)
	elif task == 2:
		search(ADMIN_DB)
	elif task == 3:
		delete_admin(ADMIN_DB)
	elif task == 4:
		exit()
	else:
		print "You have entered incorrect task number !"
		ctrlPanel()

def add(admin_db):
	print "-----------------------------------------------"
	print "You are about adding a new adminstrator account"
	admin_account = raw_input("Enter the Adminstrator name : ")
	admin_password = raw_input("Enter the Adminstrator password : ")

	if admin_db.has_key(admin_account):
		print "This account is reserved , try other"
		add(admin_db)
	else:
		admin_db[admin_account]=admin_password

	print "The admistrator account added succedfully!"
	print "Do you want to add another account ?(y/n)"
	answer = raw_input()
	if answer == "y":
		add(admin_db)
	else:
		current_db(admin_db)
		ctrlPanel()

def search(admin_db):
	searched_admin =raw_input("Enter adminstrator name : ")
	if admin_db.has_key(searched_admin):
		print "%s password is %s" %(searched_admin,admin_db[searched_admin])
		ctrlPanel()
	else:
		print "adminstrator you searched not exist !"
		ctrlPanel()

def delete_admin(admin_db):
	deleted_account = raw_input('Enter the adminstrator account you want to delete : ')
	del admin_db[deleted_account]
	print "%s is deleted successfully" %deleted_account
	ctrlPanel()

def current_db(admin_db):
	for k,v in admin_db.iteritems():
		print k, '\t',v

# unused function but important for the comming task
def check(admin_db):
	admin_name = raw_input("Enter the admin name : ")
	admin_pass = raw_input("Enter the password :")
	if admin_db[admin_name] == admin_pass:
		print 'True'
	else:
		print 'False'

if __name__ == '__main__':
	authentication = raw_input('IF YOU ARE THE MANAGER ENTER THE MANAGER PASSWORD!:')
	if authentication == MANAGER_PASSWORD:
        	print "PASSWORD IS INTACT :D"
        	ctrlPanel()
	else:
        	print 'WRONGE PASSWORD!'

